package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.EduTeacher;
import com.atguigu.serviceedu.entity.vo.TeacherQuery;
import com.atguigu.serviceedu.service.EduCourseService;
import com.atguigu.serviceedu.service.EduTeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Return
 * @since 2021-03-03
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin //解决跨域
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;


    @ApiOperation(value = "所有导师的列表")
    @GetMapping("getall")
    public R getAll(){
        List<EduTeacher> list = teacherService.list(null);
        R result = R.ok();
        result.data("items",list);
        return result;
    }

    @ApiOperation(value = "删除一个导师")
    @DeleteMapping("deleteTeacher/{id}")
    public R deleteone(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        boolean b = teacherService.removeById(id);
        if (b){
            return R.ok();
        }else{
            return R.error();
        }
    }


    @ApiOperation(value = "分页讲师列表")
    @GetMapping("pageTeacher/{page}/{limit}")
    public R pagelist(
            @ApiParam(name = "page", value = "页数", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "页面大小", required = true)
            @PathVariable Long limit
    ){
        Page<EduTeacher> pageparam = new Page<>(page,limit);
        teacherService.page(pageparam,null);
        List<EduTeacher> records = pageparam.getRecords();
        long total = pageparam.getTotal();

//        Map map = new HashMap();
//        map.put("rows", records);
//        map.put("total",total);
//        R.ok().data(map);

        return R.ok().data("rows", records).data("total",total);
    }

    @ApiOperation(value = "分页讲师列表条件查询")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pagelistCondition(
            @ApiParam(name = "current", value = "页数", required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit", value = "页面大小", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
            @RequestBody(required = false) TeacherQuery teacherQuery
    ){
       Page<EduTeacher> pageparam = new Page<>(current,limit);
       teacherService.pageQuery(pageparam,teacherQuery);
        List<EduTeacher> records = pageparam.getRecords();
        long total = pageparam.getTotal();

        return R.ok().data("rows", records).data("total",total);
    }


    //添加讲师的接口
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R save(
            @ApiParam(name = "eduTeacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher
    ){
        boolean save = teacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //根据id查询讲师
    @ApiOperation(value = "根据讲师ID查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getById(
            @ApiParam(name = "id",value = "讲师ID", required = true)
            @PathVariable String id
    ){
        EduTeacher byId = teacherService.getById(id);
        return R.ok().data("item",byId);
    }

    //根据ID修改讲师
    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("modifyTeacher/{id}")
    public R modifyById(
            @ApiParam(name = "id",value = "讲师ID", required = true)
            @PathVariable String id,
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher
    ){
        eduTeacher.setId(id);
        boolean b = teacherService.updateById(eduTeacher);
        if(b){
            return R.ok();
        }else{
            return R.error();
        }
    }



}

